<?
	include 	"include/db.inc";

	$USER_DETAILS	="";
	$ERROR_MSG	="";

	function	select_trigger_by_triggerid($triggerid)
	{
		global	$ERROR_MSG;

		$sql="select triggerid,expression,description,istrue,priority,lastchange,dep_level,comments,url from triggers where triggerid=$triggerid";
		$result=DBselect($sql);
		if(DBnum_rows($result) == 1)
		{
			return	DBfetch($result);	
		}
		else
		{
			$ERROR_MSG="No trigger with triggerid=[$triggerid]";
		}
		return	$trigger;
	}

	function	select_config()
	{
		global	$ERROR_MSG;

		$sql="select smtp_server,smtp_helo,smtp_email,password_required,alarm_history,alert_history from config";
		$result=DBselect($sql);

		if(DBnum_rows($result) == 1)
		{
			$config["smtp_server"]=DBget_field($result,0,0);
			$config["smtp_helo"]=DBget_field($result,0,1);
			$config["smtp_email"]=DBget_field($result,0,2);
			$config["password_required"]=DBget_field($result,0,3);
			$config["alarm_history"]=DBget_field($result,0,4);
			$config["alert_history"]=DBget_field($result,0,5);
		}
		else
		{
			$ERROR_MSG="Unable to select configuration";
		}
		return	$config;
	}

	function	select_host_by_hostid($hostid)
	{
		global	$ERROR_MSG;

		$sql="select hostid,host,useip,ip,port,status from hosts where hostid=$hostid";
		$result=DBselect($sql);
		if(DBnum_rows($result) == 1)
		{
			$host["hostid"]=DBget_field($result,0,0);
			$host["host"]=DBget_field($result,0,1);
			$host["useip"]=DBget_field($result,0,2);
			$host["ip"]=DBget_field($result,0,3);
			$host["port"]=DBget_field($result,0,4);
			$host["status"]=DBget_field($result,0,5);
		}
		else
		{
			$ERROR_MSG="No host with hostid=[$hostid]";
		}
		return	$host;
	}

	function	get_num_of_service_childs($serviceid)
	{
		global	$ERROR_MSG;

		$sql="select count(*) from services_links where serviceupid=$serviceid";
		$result=DBselect($sql);
		return	DBget_field($result,0,0);
	}

	function	get_service_by_serviceid($serviceid)
	{
		global	$ERROR_MSG;

		$sql="select serviceid,name,triggerid from services where serviceid=$serviceid";
		$result=DBselect($sql);
		if(Dbnum_rows($result) == 1)
		{
			return	DBfetch($result);
		}
		else
		{
			$ERROR_MSG="No service with serviceid=[$serviceid]";
		}
		return	FALSE;
	}

	function	show_messages($bool,$msg,$errmsg)
	{
		global	$ERROR_MSG;

		if(!$bool)
		{
			$msg="ERROR:".$errmsg;
			$color="#AA0000";
		}
		else
		{
			$color="#223344";
		}
		echo "<center>";
		echo "<font face='Helvetica' size=+1 color='$color'>";
		if($ERROR_MSG=="")
		{
			echo "<b>[$msg]</b>";
		}
		else
		{
			echo "<b>[$msg. $ERROR_MSG]</b>";
		}
		echo "</font>";
		echo "</center><br>";
	}

	function	show_message($msg)
	{
		show_messages(TRUE,$msg,'');
	}

	function	show_error_message($msg)
	{
		show_messages(FALSE,'',$msg);
	}

	function	validate_float($str)
	{
//		echo "Validating float:$str<br>";
		if (eregi('^([0-9]+)((\.)?)([0-9]*)$', $str, &$arr)) 
		{
			return 0;
		}
		else
		{
			return -1;
		}
	}

// Does expression match server:key.function(param) ?
	function	validate_simple_expression($expression)
	{
		global	$ERROR_MSG;

//		echo "Validating simple:$expression<br>";
//		if (eregi('^\{([0-9a-zA-Z\_\.]+)\:([0-9a-zA-Z\_]+)\.((diff)|(min)|(max)|(last)|(nodata))\(([0-9\.]+)\)\}$', $expression, &$arr)) 
		if (eregi('^\{([0-9a-zA-Z[.-.]\_\.]+)\:([]\[0-9a-zA-Z\_\/\.]+)\.((diff)|(min)|(max)|(last)|(prev))\(([0-9\.]+)\)\}$', $expression, &$arr)) 
		{
			$host=$arr[1];
			$key=$arr[2];
			$function=$arr[3];
			$parameter=$arr[9];

			$sql="select count(*) from hosts h,items i where h.host='$host' and i.key_='$key' and h.hostid=i.hostid";
			$result=DBselect($sql);
			if(DBget_field($result,0,0)!=1)
			{
				$ERROR_MSG="No such host ($host) or monitored parameter ($key)";
				return -1;
			}

			if(validate_float($parameter)!=0)
			{
				$ERROR_MSG="[$parameter] is not a float";
				return -1;
			}
		}
		else
		{
			$ERROR_MSG="Expression [$expression] does not match to [server:key.func(param)]";
			return -1;
		}
		return 0;
	}

	function	validate_expression($expression)
	{
		global	$ERROR_MSG;

//		echo "Validating expression: $expression<br>";

		$ok=0;
// Replace all {server:key.function(param)} with 0
		while($ok==0)
		{
//			echo "Expression:$expression<br>";
			$arr="";
			if (eregi('^((.)*)(\{((.)*)\})((.)*)$', $expression, &$arr)) 
			{
//				for($i=0;$i<20;$i++)
//				{
//					if($arr[$i])
//						echo "  $i: ",$arr[$i],"<br>";
//				}
				if(validate_simple_expression($arr[3])!=0)
				{
					return -1;
				}
				$expression=$arr[1]."0".$arr[6];
	                }
			else
			{
				$ok=1;
			}
		}
//		echo "Result:$expression<br><hr>";

		$ok=0;
		while($ok==0)
		{
// 	Replace all float sign float with 0
//			echo "Expression:$expression<br>";
			$arr="";
			if (eregi('^((.)*)([0-9\.]+)([\&\|\>\<\=]{1})([0-9\.]+)((.)*)$', $expression, &$arr)) 
			{
//				echo "OK<br>";
//				for($i=0;$i<50;$i++)
//				{
//					if($arr[$i]!="")
//						echo "  $i: ",$arr[$i],"<br>";
//				}
				if(validate_float($arr[3])!=0)
				{
					$ERROR_MSG="[".$arr[3]."] is not a float";
					return -1;
				}
				if(validate_float($arr[5])!=0)
				{
					$ERROR_MSG="[".$arr[5]."] is not a float";
					return -1;
				}
				$expression=$arr[1]."0".$arr[6];
	                }
			else
			{
				$ok=1;
			}


// 	Replace all (float) with 0
//			echo "Expression2:$expression<br>";
			$arr="";
			if (eregi('^((.)*)(\(([0-9\.]+)\))((.)*)$', $expression, &$arr)) 
			{
//				echo "OK<br>";
//				for($i=0;$i<30;$i++)
//				{
//					if($arr[$i]!="")
//						echo "  $i: ",$arr[$i],"<br>";
//				}
				if(validate_float($arr[4])!=0)
				{
					$ERROR_MSG="[".$arr[4]."] is not a float";
					return -1;
				}
				$expression=$arr[1]."0".$arr[5];
				$ok=0;
	                }
			else
			{
				$ok=1;
			}



		}
//		echo "Result:$expression<br><hr>";

		return $expression;
	}

	function	cr()
	{
		echo "\n";
	}

	function	check_authorisation()
	{
		global $page;
		global $PHP_AUTH_USER,$PHP_AUTH_PW;
		global $USER_DETAILS;

		$passwd=md5($PHP_AUTH_PW);

		$sql="select g.groupid,u.userid,u.alias,u.name,u.surname from users u,groups g where u.alias='$PHP_AUTH_USER' and u.passwd='$passwd' and u.groupid=g.groupid";
		$result=DBselect($sql);
		if(DBnum_rows($result)==1)
		{
			$USER_DETAILS["groupid"]=DBget_field($result,0,0);
			$USER_DETAILS["userid"]=DBget_field($result,0,1);
			$USER_DETAILS["alias"]=DBget_field($result,0,2);
			$USER_DETAILS["name"]=DBget_field($result,0,3);
			$USER_DETAILS["surname"]=DBget_field($result,0,4);
		}
		

		if(	($page["file"]=="latest.php")||
			($page["file"]=="tr_status.php")||
			($page["file"]=="tr_comments.php")||
			($page["file"]=="queue.php")||
			($page["file"]=="latestalarms.php")||
			($page["file"]=="alarms.php")||
			($page["file"]=="alerts.php")||
			($page["file"]=="history.php")||
			($page["file"]=="maps.php")||
			($page["file"]=="map.php")||
			($page["file"]=="charts.php")||
			($page["file"]=="chart.php")||
			($page["file"]=="about.php")||
			($page["file"]=="srv_status.php")||
			($page["file"]=="index.php"))
		{
			$sql="select password_required from config";
			$result=DBselect($sql);

			if(DBget_field($result,0,0)==0)
			{
				return;
			}

			$sql="select count(*) from users u,groups g where u.alias='$PHP_AUTH_USER' and u.passwd='$passwd' and u.groupid=g.groupid and (g.groupid=1 or g.groupid=2)";
		}
		else
		{
			$sql="select count(*) from users u,groups g where u.alias='$PHP_AUTH_USER' and u.passwd='$passwd' and u.groupid=g.groupid and g.groupid=1";
		}
		$result=DBselect($sql);

//		echo "==",DBget_field($result,0,0),"==";

		if(DBget_field($result,0,0)!=1)
		{
			Header("WWW-authenticate: basic realm=\"Zabbix\"");
			Header("HTTP/1.0 401 Unauthorized");
			echo "Try to contact Zabbix Administrator to get additional rights :-)\n";
			exit;
		}


	}

	# Header for HTML pages

	function	show_header($title,$refresh,$nomenu)
	{
		global $page;

		check_authorisation();

?>
	<!doctype html public "-//W3C//DTD HTML 3.2//EN">
	<html>
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
	<meta name="Author" content="Alexei Vladishev">
<?
	if($refresh!=0)
	{
		echo "<meta http-equiv=\"refresh\" content=\"$refresh\">";
		echo "<title>$title [refreshed every $refresh sec]</title>";
	}
	else
	{
		echo "<title>$title</title>";
	}
	echo "<head>";
?>
	<body bgcolor="#778899" text="#000000" link="#000000" vlink="#000000" topmargin=4 leftmargin=0 rightmargin=0 marginheight=4>
<?
		if($nomenu == 0)
		{
?>

	<table border=0 cellspacing=0 cellpadding=0 width="100%" bgcolor=000000>
	<tr>
	<td valign="top">
		<table width="100%" border=0 cellspacing=1 cellpadding=3>
		<tr>
		<td colspan=1 bgcolor=FFFFFF align=center valign="top" width="15%">
			<font face="Arial,Helvetica" size=2>
				<a href="latest.php">
<?
				if( 	($page["file"]=="latest.php") ||
					($page["file"]=="history.php"))
				{
					echo "<b>[LATEST VALUES]</b></a>";
				}
				else
				{
					echo "LATEST VALUES</a>";
				}
?>
			</font>
		</td>
		<td colspan=1 bgcolor=FFFFFF align=center valign="top" width="10%">
			<font face="Arial,Helvetica" size=2>
				<a href="tr_status.php?notitle=true&onlytrue=true&noactions=true&compact=true">
<?
				if($page["file"]=="tr_status.php")
				{
					echo "<b>[STATUS OF TRIGGERS]</b></a>";
				}
				else
				{
					echo "STATUS OF TRIGGERS</a>";
				}
?>
			</font>
		</td>
		<td colspan=1 bgcolor=FFFFFF align=center valign="top" width="10%">
			<font face="Arial,Helvetica" size=2>
				<a href="queue.php">
<?
				if($page["file"]=="queue.php")
				{
					echo "<b>[QUEUE]</b></a>";
				}
				else
				{
					echo "QUEUE</a>";
				}
?>
			</font>
		</td>
		<td colspan=1 bgcolor=FFFFFF align=center valign="top" width="10%">
			<font face="Arial,Helvetica" size=2>
				<a href="latestalarms.php">
<?
				if(($page["file"]=="latestalarms.php") ||
					($page["file"]=="alarms.php"))
				{
					echo "<b>[ALARMS]</b></a>";
				}
				else
				{
					echo "ALARMS</a>";
				}
?>
			</font>
		</td>
		<td colspan=1 bgcolor=FFFFFF align=center valign="top" width="15%">
			<font face="Arial,Helvetica" size=2>
				<a href="alerts.php">
<?
				if($page["file"]=="alerts.php")
				{
					echo "<b>[ALERT HISTORY]</b></a>";
				}
				else
				{
					echo "ALERT HISTORY</a>";
				}
?>
			</font>
		</td>
		<td colspan=1 bgcolor=FFFFFF align=center valign="top" width="15%">
			<font face="Arial,Helvetica" size=2>
				<a href="maps.php">
<?
				if($page["file"]=="maps.php")
				{
					echo "<b>[NETWORK MAPS]</b></a>";
				}
				else
				{
					echo "NETWORK MAPS</a>";
				}
?>
			</font>
		</td>
		<td colspan=1 bgcolor=FFFFFF align=center valign="top" width="15%">
			<font face="Arial,Helvetica" size=2>
				<a href="charts.php">
<?
				if($page["file"]=="charts.php")
				{
					echo "<b>[GRAPHS]</b></a>";
				}
				else
				{
					echo "GRAPHS</a>";
				}
?>
			</font>
		</td>
<? /*?>
		<td colspan=1 bgcolor=FFFFFF align=center valign="top" width="15%">
			<font face="Arial,Helvetica" size=2>
				<a href="srv_status.php">
<?
				if($page["file"]=="srv_status.php")
				{
					echo "<b>[IT SERVICES]</b></a>";
				}
				else
				{
					echo "IT SERVICES</a>";
				}
?>
			</font>
		</td>
<? */?>
		</tr>
		<tr>
		<td colspan=2 bgcolor=FFFFFF align=center valign="top" width="15%">
			<font face="Arial,Helvetica" size=2>
				<a href="about.php">
<?
				if($page["file"]=="about.php")
				{
					echo "<b>[ABOUT]</b></a>";
				}
				else
				{
					echo "ABOUT</a>";
				}
?>
			</font>
		</td>
		<td colspan=3 bgcolor=FFFFFF align=center valign="top" width="15%">
			<font face="Arial,Helvetica" size=2>
				<a href="report1.php">
<?
				if($page["file"]=="report1.php")
				{
					echo "<b>[STATUS OF ZABBIX]</b></a>";
				}
				else
				{
					echo "STATUS OF ZABBIX</a>";
				}
?>
			</font>
		</td>
		<td colspan=2 bgcolor=FFFFFF align=center valign="top" width="15%">
			<font face="Arial,Helvetica" size=2>
				<a href="report2.php">
<?
				if($page["file"]=="report2.php")
				{
					echo "<b>[AVAILABILITY REPORT]</b></a>";
				}
				else
				{
					echo "AVAILABILITY REPORT</a>";
				}
?>
			</font>
		</td>
		</tr>
		<tr>
		<td colspan=1 bgcolor=FFFFFF align=center valign="top" width="15%">
			<font face="Arial,Helvetica" size=2>
				<a href="config.php">
<?
				if($page["file"]=="config.php")
				{
					echo "<b>[CONFIG]</b></a>";
				}
				else
				{
					echo "CONFIG</a>";
				}
?>
			</font>
		</td>
		<td colspan=1 bgcolor=FFFFFF align=center valign="top" width="10%">
			<font face="Arial,Helvetica" size=2>
				<a href="users.php">
<?
				if(	($page["file"]=="users.php")||
					($page["file"]=="media.php"))
				{
					echo "<b>[USERS]</b></a>";
				}
				else
				{
					echo "USERS</a>";
				}
?>
			</font>
		</td>
		<td colspan=1 bgcolor=FFFFFF align=center valign="top" width="10%">
			<font face="Arial,Helvetica" size=2>
				<a href="hosts.php">
<?
				if($page["file"]=="hosts.php")
				{
					echo "<b>[HOSTS]</b></a>";
				}
				else
				{
					echo "HOSTS</a>";
				}
?>
			</font>
		</td>
		<td colspan=1 bgcolor=FFFFFF align=center valign="top" width="10%">
			<font face="Arial,Helvetica" size=2>
				<a href="items.php">
<?
				if($page["file"]=="items.php")
				{
					echo "<b>[ITEMS]</b></a>";
				}
				else
				{
					echo "ITEMS</a>";
				}
?>
			</font>
		</td>
		<td colspan=1 bgcolor=FFFFFF align=center valign="top" width="15%">
			<font face="Arial,Helvetica" size=2>
				<a href="triggers.php">
<?
				if(	($page["file"]=="triggers.php")||
					($page["file"]=="actions.php"))
				{
					echo "<b>[TRIGGERS]</b></a>";
				}
				else
				{
					echo "TRIGGERS</a>";
				}
?>
			</font>
		</td>
		<td colspan=1 bgcolor=FFFFFF align=center valign="top" width="15%">
			<font face="Arial,Helvetica" size=2>
				<a href="sysmaps.php">
<?
				if(	($page["file"]=="sysmaps.php")||
					($page["file"]=="sysmap.php"))
				{
					echo "<b>[NETWORK MAPS]</b></a>";
				}
				else
				{
					echo "NETWORK MAPS</a>";
				}
?>
			</font>
		</td>
		<td colspan=1 bgcolor=FFFFFF align=center valign="top" width="15%">
			<font face="Arial,Helvetica" size=2>
				<a href="graphs.php">
<?
				if(	($page["file"]=="graphs.php")||
					($page["file"]=="graph.php"))
				{
					echo "<b>[GRAPHS]</b></a>";
				}
				else
				{
					echo "GRAPHS</a>";
				}
?>
			</font>
		</td>
<? /*?>
		<td colspan=1 bgcolor=FFFFFF align=center valign="top" width="15%">
			<font face="Arial,Helvetica" size=2>
				<a href="services.php">
<?
				if($page["file"]=="services.php")
				{
					echo "<b>[IT SERVICES]</b></a>";
				}
				else
				{
					echo "IT SERVICES</a>";
				}
?>
			</font>
		</td>
<?*/?>
		</tr>
		</table>
	</td>
	</tr>
	</table>

	<br>
<?
		}
	}

	# Show values in plain text

	function	show_plaintext($itemid, $from, $till)
	{
		$result=DBselect("select clock,value from history where itemid=$itemid and clock>$from and clock<$till order by clock");

		echo "<PRE>";
		for($i=0;$i<DBnum_rows($result);$i++)
		{
			$clock=DBget_field($result,$i,0);
			$value=DBget_field($result,$i,1);
			echo date("Y-m-d H:i:s",$clock);
			echo "\t$clock\t$value\n";
		}
	}
 

	# Translate {10}>10 to something like localhost:procload.last(0)>10

	function	explode_exp ($expression, $html)
	{
#		echo "EXPRESSION:",$expression,"<Br>";

		$functionid='';
		$exp='';
		$state='';
		for($i=0;$i<strlen($expression);$i++)
		{
			if($expression[$i] == '{')
			{
				$functionid='';
				$state='FUNCTIONID';
				continue;
			}
			if($expression[$i] == '}')
			{
				$state='';
				$sql="select h.host,i.key_,f.function,f.parameter,i.itemid from items i,functions f,hosts h where functionid=$functionid and i.itemid=f.itemid and h.hostid=i.hostid";
				$res1=DBselect($sql);
				if($html == 0)
				{
					$exp=$exp."{".DBget_field($res1,0,0).":".DBget_field($res1,0,1).".".DBget_field($res1,0,2)."(".DBget_field($res1,0,3).")}";
				}
				else
				{
					$exp=$exp."{<A HREF=\"history.php?action=showhistory&itemid=".DBget_field($res1,0,4)."\">".DBget_field($res1,0,0).":".DBget_field($res1,0,1)."</A>.<B>".DBget_field($res1,0,2)."(</B>".DBget_field($res1,0,3)."<B>)</B>}";
				}
				continue;
			}
			if($state == "FUNCTIONID")
			{
				$functionid=$functionid.$expression[$i];
				continue;
			}
			$exp=$exp.$expression[$i];
		}
#		echo "EXP:",$exp,"<Br>";
		return $exp;
	}

	# Translate localhost:procload.last(0)>10 to {12}>10

	function	implode_exp ($expression, $triggerid)
	{
//		echo "Expression:$expression<br>";
		$exp='';
		$state="";
		for($i=0;$i<strlen($expression);$i++)
		{
			if($expression[$i] == '{')
			{
				if($state=="")
				{
					$host='';
					$key='';
					$function='';
					$parameter='';
					$state='HOST';
					continue;
				}
			}
			if( ($expression[$i] == '}')&&($state=="") )
			{
//				echo "HOST:$host<BR>";
//				echo "KEY:$key<BR>";
//				echo "FUNCTION:$function<BR>";
//				echo "PARAMETER:$parameter<BR>";
				$state='';
		
				$sql="select i.itemid from items i,hosts h where i.key_='$key' and h.host='$host' and h.hostid=i.hostid";
#				echo $sql,"<Br>";
				$res=DBselect($sql);

				$itemid=DBget_field($res,0,0);
#				echo "ITEMID:$itemid<BR>";
	
#				$sql="select functionid,count(*) from functions where function='$function' and parameter=$parameter group by 1";
#				echo $sql,"<Br>";
#				$res=DBselect($sql);
#
#				if(DBget_field($res,0,1)>0)
#				{
#					$functionid=DBget_field($res,0,0);
#				}
#				else
#				{
					$sql="insert into functions (itemid,triggerid,function,parameter) values ($itemid,$triggerid,'$function',$parameter)";
#					echo $sql,"<Br>";
					$res=DBexecute($sql);
					if(!$res)
					{
#						echo "ERROR<br>";
						return	$res;
					}
					$functionid=DBinsert_id($res,"functions","functionid");
#				}
#				echo "FUNCTIONID:$functionid<BR>";

				$exp=$exp.'{'.$functionid.'}';

				continue;
			}
			if($expression[$i] == '(')
			{
				if($state == "FUNCTION")
				{
					$state='PARAMETER';
					continue;
				}
			}
			if($expression[$i] == ')')
			{
				if($state == "PARAMETER")
				{
					$state='';
					continue;
				}
			}
			if($expression[$i] == ':')
			{
				$state="KEY";
				continue;
			}
			if($expression[$i] == '.')
			{
				if($state == "KEY")
				{
					$state="FUNCTION";
					continue;
				}
				// Support for '.' in KEY
				if($state == "FUNCTION")
				{
					$state="FUNCTION";
					$key=$key.".".$function;
					$function="";
					continue;
				}
			}
			if($state == "HOST")
			{
				$host=$host.$expression[$i];
				continue;
			}
			if($state == "KEY")
			{
				$key=$key.$expression[$i];
				continue;
			}
			if($state == "FUNCTION")
			{
				$function=$function.$expression[$i];
				continue;
			}
			if($state == "PARAMETER")
			{
				$parameter=$parameter.$expression[$i];
				continue;
			}
			$exp=$exp.$expression[$i];
		}
		return $exp;
	}

	function	update_trigger_comments($triggerid,$comments)
	{
		$comments=addslashes($comments);
		$sql="update triggers set comments='$comments' where triggerid=$triggerid";
		return	DBexecute($sql);
	}

	# Update Trigger status

	function	update_trigger_status($triggerid,$status)
	{
		$sql="update triggers set istrue=$status where triggerid=$triggerid";
		return	DBexecute($sql);
	}

	# Update Item status

	function	update_item_status($itemid,$status)
	{
		$sql="update items set status=$status where itemid=$itemid";
		return	DBexecute($sql);
	}

	# Update Host status

	function	update_host_status($hostid,$status)
	{
		$sql="update hosts set status=$status where hostid=$hostid";
		return	DBexecute($sql);
	}

	# Update Item definition

	function	update_item($itemid,$description,$key,$hostid,$delay,$history,$status,$type,$snmp_community,$snmp_oid)
	{
		$sql="update items set description='$description',key_='$key',hostid=$hostid,delay=$delay,history=$history,lastdelete=0,nextcheck=0,status=$status,type=$type,snmp_community='$snmp_community',snmp_oid='$snmp_oid' where itemid=$itemid";
		return	DBexecute($sql);
	}

	# Add Action

	function	add_action( $triggerid, $userid, $good, $delay, $subject, $message )
	{
		$sql="insert into actions (triggerid,userid,good,delay,nextcheck,subject,message) values ($triggerid,$userid,$good,$delay,0,'$subject','$message')";
		return	DBexecute($sql);
	}

	function	service_has_parent($serviceid)
	{
		$sql="select count(*) from services_links where servicedownid=$serviceid";
		$result=DBselect($sql);
		if(DBget_field($result,0,0)>0)
		{
			return	TRUE;
		}
		return	FALSE;
	}

	function	service_has_no_this_parent($parentid,$serviceid)
	{
		$sql="select count(*) from services_links where serviceupid=$parentid and servicedownid=$serviceid";
		$result=DBselect($sql);
		if(DBget_field($result,0,0)>0)
		{
			return	FALSE;
		}
		return	TRUE;
	}


	function	delete_service($serviceid)
	{
		$sql="delete from services_links where servicedownid=$serviceid or serviceupid=$serviceid";
		$result=DBexecute($sql);
		if(!$result)
		{
			return	$result;
		}
		$sql="delete from services where serviceid=$serviceid";
		return DBexecute($sql);
	}

	function	update_service($serviceid,$name,$triggerid,$linktrigger)
	{
		if( isset($linktrigger)&&($linktrigger=="on") )
		{
			$sql="update services set name='$name',triggerid=$triggerid,status=0 where serviceid=$serviceid";
		}
		else
		{
			$sql="update services set name='$name',triggerid=NULL,status=0 where serviceid=$serviceid";
		}
		return	DBexecute($sql);
	}

	function	add_service($name,$triggerid,$linktrigger)
	{
		if( isset($linktrigger)&&($linktrigger=="on") )
		{
			$sql="insert into services (serviceid,name,triggerid,status) values (NULL,'$name',$triggerid,0)";
		}
		else
		{
			$sql="insert into services (serviceid,name,status) values (NULL,'$name',0)";
		}
		return	DBexecute($sql);
	}

	function	add_service_link($servicedownid,$serviceupid,$softlink)
	{
		if(isset($softlink) && ($softlink=="on"))
		{
			$sql="insert into services_links (servicedownid,serviceupid,soft) values ($servicedownid,$serviceupid,1)";
		}
		else
		{
			$sql="insert into services_links (servicedownid,serviceupid,soft) values ($servicedownid,$serviceupid,0)";
		}
		return	DBexecute($sql);
	}

	# Update Action

	function	update_action( $actionid, $userid, $good, $delay, $subject, $message )
	{
		$sql="update actions set userid=$userid,good=$good,delay=$delay,nextcheck=0,subject='$subject',message='$message' where actionid=$actionid";
		return	DBexecute($sql);
	}

	function	delete_graphs_item($gitemid)
	{
		$sql="delete from graphs_items where gitemid=$gitemid";
		return	DBexecute($sql);
	}

	# Delete Graph

	function	delete_graph($graphid)
	{
		$sql="delete from graphs_items where graphid=$graphid";
		$result=DBexecute($sql);
		if(!$result)
		{
			return	$result;
		}
		$sql="delete from graphs where graphid=$graphid";
		return	DBexecute($sql);
	}

	# Delete System Map

	function	delete_sysmap( $sysmapid )
	{
		$sql="delete from sysmaps where sysmapid=$sysmapid";
		$result=DBexecute($sql);
		if(!$result)
		{
			return	$result;
		}
		$sql="delete from sysmaps_hosts where sysmapid=$sysmapid";
		$result=DBexecute($sql);
		if(!$result)
		{
			return	$result;
		}
		$sql="delete from sysmaps_links where sysmapid=$sysmapid";
		return	DBexecute($sql);
	}

	# Delete Alert by actionid

	function	delete_alert_by_actionid( $actionid )
	{
		$sql="delete from alerts where actionid=$actionid";
		return	DBexecute($sql);
	}

	# Delete Action by userid

	function	delete_actions_by_userid( $userid )
	{
		$sql="select actionid from actions where userid=$userid";
		$result=DBexecute($sql);
		for($i=0;$i<DBnum_rows($result);$i++)
		{
			$actionid=DBget_field($result,$i,0);
			delete_alert_by_actionid($actionid);
		}

		$sql="delete from actions where userid=$userid";
		return	DBexecute($sql);
	}

	# Delete Action

	function	delete_action( $actionid )
	{
		$sql="delete from actions where actionid=$actionid";
		$result=DBexecute($sql);

		return delete_alert_by_actionid($actionid);
	}

	# Delete from History

	function	delete_history_by_itemid( $itemid )
	{

		$sql="delete from history where itemid=$itemid";
		return	DBexecute($sql);
	}

	function	delete_trigger_dependency($triggerid_down,$triggerid_up)
	{

		$sql="select count(*) from trigger_depends where triggerid_down=$triggerid_up and triggerid_up=$triggerid_down";
		$result=DBexecute($sql);
		if(DBget_field($result,0,0)>0)
		{
			return	FALSE;
		}

		$sql="select triggerid_down,triggerid_up from trigger_depends where triggerid_up=$triggerid_up or triggerid_down=$triggerid_down";
		$result=DBexecute($sql);
		for($i=0;$i<DBnum_rows($result);$i++)
		{
			$down=DBget_field($result,$i,0);
			$up=DBget_field($result,$i,1);
			$sql="delete from trigger_depends where triggerid_up=$up and triggerid_down=$down";
			DBexecute($sql);
			$sql="update triggers set dep_level=dep_level-1 where triggerid=$up";
			DBexecute($sql);
		}
		return	TRUE;
	}

	function	insert_dependency($triggerid_down,$triggerid_up)
	{
		$sql="insert into trigger_depends (triggerid_down,triggerid_up) values ($triggerid_down,$triggerid_up)";
		$result=DBexecute($sql);
		if(!$result)
		{
			return	$result;
		}
		$sql="update triggers set dep_level=dep_level+1 where triggerid=$triggerid_up";
		$result=DBexecute($sql);
		return	$result;
	}

	// If 1 depends on 2, and 2 depends on 3, then add dependency 1->3
	function	add_additional_dependencies($triggerid_down,$triggerid_up)
	{
		$sql="select triggerid_down from trigger_depends where triggerid_up=$triggerid_down";
		$result=DBselect($sql);
		for($i=0;$i<DBnum_rows($result);$i++)
		{
			$triggerid=DBget_field($result,$i,0);
			insert_dependency($triggerid,$triggerid_up);
			add_additional_dependencies($triggerid,$triggerid_up);
		}
		$sql="select triggerid_up from trigger_depends where triggerid_down=$triggerid_up";
		$result=DBselect($sql);
		for($i=0;$i<DBnum_rows($result);$i++)
		{
			$triggerid=DBget_field($result,$i,0);
			insert_dependency($triggerid_down,$triggerid);
			add_additional_dependencies($triggerid_down,$triggerid);
		}
	}

	function	add_trigger_dependency($triggerid,$depid)
	{
		$result=insert_dependency($triggerid,$depid);;
		if(!$result)
		{
			return $result;
		}
		add_additional_dependencies($triggerid,$depid);
		return $result;
	}

	# Delete Function definition

	# Add Item definition

	function	add_item($description,$key,$hostid,$delay,$history,$status,$type,$snmp_community,$snmp_oid)
	{
		$sql="insert into items (description,key_,hostid,delay,history,lastdelete,nextcheck,status,type,snmp_community,snmp_oid) values ('$description','$key',$hostid,$delay,$history,0,0,$status,$type,'$snmp_community','$snmp_oid')";
		$result=DBexecute($sql);
		return DBinsert_id($result,"items","itemid");
	}

	# Delete Function definition

	function	delete_function_by_triggerid($triggerid)
	{
		$sql="delete from functions where triggerid=$triggerid";
		return	DBexecute($sql);
	}

	function	delete_actions_by_triggerid($triggerid)
	{
		$sql="delete from actions where triggerid=$triggerid";
		return	DBexecute($sql);
	}

	function	delete_alarms_by_triggerid($triggerid)
	{
		$sql="delete from alarms where triggerid=$triggerid";
		return	DBexecute($sql);
	}

	# Delete Function and Trigger definitions by itemid

	function	delete_triggers_by_itemid($itemid)
	{
		$sql="select triggerid from functions where itemid=$itemid";
		$result=DBselect($sql);
		for($i=0;$i<DBnum_rows($result);$i++)
		{
			if(!delete_trigger(DBget_field($result,$i,0)))
			{
				return FALSE;
			}
		}
		$sql="delete from functions where itemid=$itemid";
		return	DBexecute($sql);

	}

	# Delete Item definition

	function	delete_item($itemid)
	{
		$sql="select hostid from items where itemid=$itemid";
		$result=DBselect($sql);
		$hostid=DBget_field($result,0,0);
		delete_sysmaps_host($hostid);

		$result=delete_triggers_by_itemid($itemid);
		if(!$result)
		{
			return	$result;
		}
		$result=delete_history_by_itemid($itemid);
		$sql="delete from graphs_items where itemid=$itemid";
		if(!$result)
		{
			return	$result;
		}
		$result=DBexecute($sql);
		if(!$result)
		{
			return	$result;
		}
		$sql="delete from items where itemid=$itemid";
		return	DBexecute($sql);
	}

	# Add alarm

	function	add_alarm($triggerid,$istrue)
	{
		$now=time();
		$sql="insert into alarms(triggerid,clock,istrue) values($triggerid,$now,$istrue)";
		return	DBexecute($sql);
	}

	# Add Trigger definition

	function	add_trigger($expression,$description,$priority,$istrue,$comments,$url)
	{
		$description=addslashes($description);
		$sql="insert into triggers  (description,priority,istrue,comments,url) values ('$description',$priority,$istrue,'$comments','$url')";
#		echo $sql,"<br>";
		$result=DBexecute($sql);
		if(!$result)
		{
			return	$result;
		}
 
		$triggerid=DBinsert_id($result,"triggers","triggerid");
#		echo $triggerid,"<br>";
 
		$expression=implode_exp($expression,$triggerid);
		$sql="update triggers set expression='$expression' where triggerid=$triggerid";
#		echo $sql,"<br>";
		return	DBexecute($sql);
	}

	# Delete Trigger definition

	function	delete_trigger($triggerid)
	{
		global	$ERROR_MSG;

		$sql="select count(*) from trigger_depends where triggerid_down=$triggerid or triggerid_up=$triggerid";
		$result=DBexecute($sql);
		if(DBget_field($result,0,0)>0)
		{
			$ERROR_MSG="Delete dependencies first";
			return	FALSE;
		}

		$result=delete_function_by_triggerid($triggerid);
		if(!$result)
		{
			return	$result;
		}
		$result=delete_alarms_by_triggerid($triggerid);
		if(!$result)
		{
			return	$result;
		}
		$result=delete_actions_by_triggerid($triggerid);
		if(!$result)
		{
			return	$result;
		}

		$sql="delete from triggers where triggerid=$triggerid";
		return	DBexecute($sql);
	}

	# Update Trigger definition

	function	update_trigger($triggerid,$expression,$description,$priority,$istrue,$comments,$url)
	{

		$result=delete_function_by_triggerid($triggerid);
		if(!$result)
		{
			return	$result;
		}

		$expression=implode_exp($expression,$triggerid);
		if($istrue != 2) $istrue="istrue";
		$sql="update triggers set expression='$expression',description='$description',priority=$priority,istrue=$istrue,comments='$comments',url='$url' where triggerid=$triggerid";
		return	DBexecute($sql);
	}

	# Update User definition

	function	update_user($userid,$groupid,$name,$surname,$alias,$passwd)
	{
		if($passwd=="")
		{
			$sql="update users set groupid=$groupid,name='$name',surname='$surname',alias='$alias' where userid=$userid";
		}
		else
		{
			$passwd=md5($passwd);
			$sql="update users set groupid=$groupid,name='$name',surname='$surname',alias='$alias',passwd='$passwd' where userid=$userid";
		}
		return DBexecute($sql);
	}


	# Add User definition

	function	add_user($groupid,$name,$surname,$alias,$passwd)
	{
		$passwd=md5($passwd);
		$sql="insert into users (groupid,name,surname,alias,passwd) values ($groupid,'$name','$surname','$alias','$passwd')";
		return DBexecute($sql);
	}

	# Add Items and Triggers from template

	function	add_from_templates($hostid,$host)
	{
		$result=DBselect("select itemtemplateid,description,key_,delay from items_template");
		for($i=0;$i<DBnum_rows($result);$i++) 
		{
			$itemtemplateid=DBget_field($result,$i,0);
			$description=DBget_field($result,$i,1);
			$key=DBget_field($result,$i,2);
			$delay=DBget_field($result,$i,3);

			$itemid=add_item($description,$key,$hostid,$delay,30*24*3600,0,0,"","");

			$result2=DBselect("select triggertemplateid,description,expression from triggers_template where itemtemplateid=$itemtemplateid");
			for($j=0;$j<DBnum_rows($result2);$j++)
			{
				$itemtemplateid=DBget_field($result2,$j,0);
				$description=DBget_field($result2,$j,1);
				$expression=DBget_field($result2,$j,2);

				for($z=0;$z<strlen($expression);$z++)
				{
					if($expression[$z] == ':')
					{
						$expression=substr($expression,0,$z)."$host:$key".substr($expression,$z+1);
						
						break;
					}
				}
				$description=sprintf($description,$host);

				add_trigger($expression,$description,0,0,"","");
			}
		}
		return	TRUE;
	}

	# Update Graph

	function	update_graph($graphid,$name,$width,$height)
	{
		$sql="update graphs set name='$name',width=$width,height=$height where graphid=$graphid";
		return	DBexecute($sql);
	}

	# Update System Map

	function	update_sysmap($sysmapid,$name,$width,$height)
	{
		$sql="update sysmaps set name='$name',width=$width,height=$height where sysmapid=$sysmapid";
		return	DBexecute($sql);
	}

	# Add Graph

	function	add_graph($name,$width,$height)
	{
		$sql="insert into graphs (name,width,height) values ('$name',$width,$height)";
		return	DBexecute($sql);
	}

	function	add_item_to_graph($graphid,$itemid,$color)
	{
		$sql="insert into graphs_items (graphid,itemid,color) values ($graphid,$itemid,'$color')";
		return	DBexecute($sql);
	}

	# Add System Map

	function	add_sysmap($name,$width,$height)
	{
		$sql="insert into sysmaps (name,width,height) values ('$name',$width,$height)";
		return	DBexecute($sql);
	}

	function	add_link($sysmapid,$shostid1,$shostid2)
	{
		$sql="insert into sysmaps_links (sysmapid,shostid1,shostid2) values ($sysmapid,$shostid1,$shostid2)";
		return	DBexecute($sql);
	}

	function	delete_link($linkid)
	{
		$sql="delete from sysmaps_links where linkid=$linkid";
		return	DBexecute($sql);
	}

	# Add Host to system map

	function add_host_to_sysmap($sysmapid,$hostid,$label,$x,$y,$icon)
	{
		$sql="insert into sysmaps_hosts (sysmapid,hostid,label,x,y,icon) values ($sysmapid,$hostid,'$label',$x,$y,'$icon')";
		return	DBexecute($sql);
	}

	function	update_sysmap_host($shostid,$sysmapid,$hostid,$label,$x,$y,$icon)
	{
		$sql="update sysmaps_hosts set hostid=$hostid,label='$label',x=$x,y=$y,icon='$icon' where shostid=$shostid";
		return	DBexecute($sql);
	}

	# Add Host definition

	function	add_host($host,$port,$status,$template,$useip,$ip)
	{
		if( isset($useip) && ($useip=="on") )
		{
			$useip=1;
		}
		else
		{
			$useip=0;
		}
		$sql="insert into hosts (host,port,status,useip,ip,disable_until) values ('$host',$port,$status,$useip,'$ip',0)";
		$result=DBexecute($sql);
		if(!$result)
		{
			return	$result;
		}
		
		$hostid=DBinsert_id($result,"hosts","hostid");

		if($template=="true")
		{
			$result=add_from_templates($hostid,$host);
		}
		return	$result;
	}

	function	update_host($hostid,$host,$port,$status,$useip,$ip)
	{
		if($useip=="on")
		{
			$useip=1;
		}
		else
		{
			$useip=0;
		}
		$sql="update hosts set host='$host',port=$port,status=$status,useip=$useip,ip='$ip' where hostid=$hostid";
		$result=DBexecute($sql);
		return	$result;
	}


	# Add Media definition

	function	add_media( $userid, $type, $sendto)
	{
		$sql="insert into media (userid,type,sendto,active) values ($userid,'$type','$sendto',0)";
		return	DBexecute($sql);
	}

	# Delete Media definition

	function	delete_media($mediaid)
	{
		$sql="delete from media where mediaid=$mediaid";
		return	DBexecute($sql);
	}

	# Delete Media definition by userid

	function	delete_media_by_userid($userid)
	{
		$sql="delete from media where userid=$userid";
		return	DBexecute($sql);
	}

	# Update configuration

	function	update_config($smtp_server,$smtp_helo,$smtp_email,$password_required,$alarm_history,$alert_history)
	{
		$sql="update config set smtp_server='$smtp_server',smtp_helo='$smtp_helo',smtp_email='$smtp_email',password_required=$password_required,alarm_history=$alarm_history,alert_history=$alert_history";
		return	DBexecute($sql);
	}


	# Activate Media

	function	activate_media($mediaid)
	{
		$sql="update media set active=0 where mediaid=$mediaid";
		return	DBexecute($sql);
	}

	# Disactivate Media

	function	disactivate_media($mediaid)
	{
		$sql="update media set active=1 where mediaid=$mediaid";
		return	DBexecute($sql);
	}

	# Delete Host from sysmap definition

	function	delete_sysmaps_host($shostid)
	{
		$sql="delete from sysmaps_hosts where shostid=$shostid";
		$result=DBexecute($sql);
		if(!$result)
		{
			return	$result;
		}
		$sql="delete from sysmaps_links where shostid1=$shostid or shostid2=$shostid";
		return	DBexecute($sql);
	}

	# Delete Host

	function	delete_host($hostid)
	{
		$sql="select itemid from items where hostid=$hostid";
		$result=DBselect($sql);
		if(!$result)
		{
			return	$result;
		}
		for($i=0;$i<DBnum_rows($result);$i++)
		{
			if(!delete_item(DBget_field($result,$i,0)))
			{
				return	FALSE;
			}
		}
		$sql="delete from hosts where hostid=$hostid";
		return	DBexecute($sql);
	}

	# Delete User definition

	function	delete_user($userid)
	{
		delete_media_by_userid($userid);
		delete_actions_by_userid($userid);

		$sql="delete from users where userid=$userid";
		return DBexecute($sql);
	}

	function	show_table_h_delimiter()
	{
		echo "</font>";
		cr();
		echo "</td>";
		cr();
		echo "<td colspan=1 bgcolor=FFFFFF align=center valign=\"top\">";
		cr();
		echo "	<font face=\"Arial,Helvetica\" size=2>";
		cr();
	}

	function	show_table2_h_delimiter()
	{
		echo "</font>";
		cr();
		echo "</td>";
		cr();
		echo "<td colspan=1 bgcolor=CCCCCC align=left valign=\"top\">";
		cr();
		echo "	<font face=\"Arial,Helvetica\" size=-1>";
		cr();
	}

	function	show_table_v_delimiter()
	{
		echo "</font>";
		cr();
		echo "</td>";
		cr();
		echo "</tr>";
		cr();
		echo "<tr>";
		cr();
		echo "<td colspan=1 bgcolor=FFFFFF align=center valign=\"top\">";
		cr();
		echo "<font face=\"Arial,Helvetica\" size=2>";
		cr();
	}

	function	show_table2_v_delimiter()
	{
		echo "</font>";
		cr();
		echo "</td>";
		cr();
		echo "</tr>";
		cr();
		echo "<tr>";
		cr();
		echo "<td colspan=1 bgcolor=CCCCCC align=left valign=\"top\">";
		cr();
		echo "<font face=\"Arial,Helvetica\" size=-1>";
		cr();
	}

	function	show_table3_v_delimiter()
	{
		echo "</font>";
		cr();
		echo "</td>";
		cr();
		echo "</tr>";
		cr();
		echo "<tr>";
		cr();
		echo "<td colspan=1 bgcolor=99AABB align=left valign=\"top\">";
		cr();
		echo "<font face=\"Arial,Helvetica\" size=-1>";
		cr();
	}


	function	show_table2_v_delimiter2()
	{
		echo "</font>";
		cr();
		echo "</td>";
		cr();
		echo "</tr>";
		cr();
		echo "<tr>";
		cr();
		echo "<td colspan=2 bgcolor=\"99AABB\" align=right valign=\"top\">";
		cr();
		echo "<font face=\"Arial,Helvetica\" size=-1>";
		cr();
	}



	function	show_table2_header_begin()
	{
		echo "<center>";
		cr();
		echo "<table border=0 cellspacing=0 cellpadding=0 width=\"50%\" bgcolor=000000>";
		cr();
		echo "<tr>";
		cr();
		echo "<td valign=\"top\">";
		cr();
		echo "<table width=\"100%\" border=0 cellspacing=1 cellpadding=3>";
		cr();
		echo "<tr>";
		cr();
		echo "<td colspan=2 bgcolor=99AABB align=center valign=\"top\">";
		cr();
		echo "	<font face=\"Arial,Helvetica\" size=+1>";
		cr();
	}

	function	show_table_header_begin()
	{
		echo "<table border=0 cellspacing=0 cellpadding=0 width=\"100%\" bgcolor=000000>";
		cr();
		echo "<tr>";
		cr();
		echo "<td valign=\"top\">";
		cr();
		echo "<table width=\"100%\" border=0 cellspacing=1 cellpadding=3>";
		cr();
		echo "<tr>";
		cr();
		echo "<td colspan=1 bgcolor=99AABB align=center valign=\"top\">";
		cr();
		echo "	<font face=\"Arial,Helvetica\" size=+1>";
		cr();
	}

	function	show_table2_header_end()
	{
		echo "	</font>";
		cr();
		echo "</td>";
		cr();
		echo "</tr>";
		cr();
		echo "</table>";
		cr();
		echo "</td>";
		cr();
		echo "</tr>";
		cr();
		echo "</table>";
		cr();
		echo "</center>";
		cr();
	}

	function	show_table_header_end()
	{
		echo "	</font>";
		cr();
		echo "</td>";
		cr();
		echo "</tr>";
		cr();
		echo "</table>";
		cr();
		echo "</td>";
		cr();
		echo "</tr>";
		cr();
		echo "</table>";
		cr();
	}

	function	show_table_header($title)
	{
		show_table_header_begin();
		cr();
		echo $title;
		cr();
		show_table_header_end();
		cr();
	}

	# Show History Graph

	function	show_history($itemid,$from,$period)
	{
		if (!isset($from))
		{
			$from=0;
			$till="NOW";
		}
		else
		{
			$till=time(NULL)-$from*3600;
			$till=date("d M - H:i:s",$till);   
		}

		if (!isset($period))
		{ 
			$period=3600;
			show_table_header("TILL $till (LAST HOUR)");
		}
		else
		{
			$tmp=$period/3600;
			show_table_header("TILL $till ($tmp HOURs)");
		}
		echo("<hr>");
		echo "<center>";
		echo "<IMG SRC=\"chart.php?itemid=$itemid&period=$period&from=$from\">"; 
		echo "</center>";
		echo("<hr>");

 
		echo "<center>";
		//  Start of <<< WEEK >>> 
		$tmp=$from+12*14;
		echo("[<A HREF=\"history.php?action=showhistory&itemid=$itemid&from=$tmp&period=$period\">");
		echo("Week back</A>] ");

		//  Start of <<< 12h >>> 
		$tmp=$from+12;
		echo("[<A HREF=\"history.php?action=showhistory&itemid=$itemid&from=$tmp&period=$period\">");
		echo("12h back</A>] ");
		//  End of <<< 12h >>> 
		
		//  HOUR BACK
		$tmp=$from+1;
		echo("[<A HREF=\"history.php?action=showhistory&itemid=$itemid&from=$tmp&period=$period\">");
		echo("1h back</A>] ");

		//  Start of --- 
		$tmp=$period+3600;
		echo("[<A HREF=\"history.php?action=showhistory&itemid=$itemid&from=$from&period=$tmp\">");
		echo("+1h</A>] ");

		if ($period>3600) 
		{
			$tmp=$period-3600;
			echo("[<A HREF=\"history.php?action=showhistory&itemid=$itemid&from=$from&period=$tmp\">");
			echo("-1h</A>] ");
		}
		else
		{
			echo("[-1h]");
		}
		//  End of ------ 

		if ($from>0) // HOUR FORWARD
		{
			$tmp=$from-1;
			echo("[<A HREF=\"history.php?action=showhistory&itemid=$itemid&from=$tmp&period=$period\">");
			echo("1h forward</A>] ");
		}
		else
		{
			echo("[1h forward]");  
		}


		if (isset($From) && ($From>0))
		{
			$tmp=$from-12;
			echo("[<A HREF=\"history.php?action=showhistory&itemid=$itemid&from=$tmp&period=$period\">");
			echo("12h forward</A>] ");
		}
		else
		{
			echo("[12h forward]");
		}

		if (isset($From) && ($From>0))
		{
			$tmp=$from-12*14;
			echo("[<A HREF=\"history.php?action=showhistory&itemid=$itemid&from=$tmp&period=$period\">");
			echo("Week forward</A>] ");
		}
		else
		{
			echo("[Week forward]");
		}
		echo "</center>";
	}

	# Show history
	function	show_freehist($itemid,$period)
	{

		echo "<br>";
		show_table2_header_begin();
		echo "Choose period";

		show_table2_v_delimiter();
		echo "<form method=\"post\" action=\"history.php\">";
		echo "<input name=\"itemid\" type=\"hidden\" value=$itemid size=8>";
		echo "Period in seconds";
		show_table2_h_delimiter();
		echo "<input name=\"period\" value=\"7200\" size=8>";

		show_table2_v_delimiter();
		echo "From (in hours)";
		show_table2_h_delimiter();
		echo "<input name=\"from\" value=\"24\" size=8>";

		show_table2_v_delimiter2();
		echo "Press ";
		echo "<input type=\"submit\" name=\"action\" value=\"showvalues\"> to see graph";

		show_table2_header_end();

		show_footer();
	}

	# Show in plain text
	function	show_plaintxt($itemid,$period)
	{
		show_table2_header_begin();
		echo "Data in plain text format";

		show_table2_v_delimiter();
		echo "<form method=\"get\" action=\"history.php\">";
		echo "<input name=\"itemid\" type=\"Hidden\" value=$itemid size=8>";
		echo "<input name=\"itemid\" type=\"Hidden\" value=$itemid size=8>";
		echo "From: (yyyy/mm/dd - HH:MM)";
		show_table2_h_delimiter();
		echo "<input name=\"fromyear\" value=\"",date("Y"),"\" size=5>/";
		echo "<input name=\"frommonth\" value=\"",date("m"),"\" size=3>/";
		echo "<input name=\"fromday\" value=\"",date("d"),"\" size=3> - ";
		echo "<input name=\"fromhour\" value=\"0\" size=3>:";
		echo "<input name=\"frommin\" value=\"00\" size=3>";

		show_table2_v_delimiter();
		echo "Till: (yyyy/mm/dd - HH:MM)";
		show_table2_h_delimiter();
		echo "<input name=\"tillyear\" value=\"",date("Y"),"\" size=5>/";
		echo "<input name=\"tillmonth\" value=\"",date("m"),"\" size=3>/";
		echo "<input name=\"tillday\" value=\"",date("d"),"\" size=3> - ";
		echo "<input name=\"tillhour\" value=\"23\" size=3>:";
		echo "<input name=\"tillmin\" value=\"59\" size=3>";

		show_table2_v_delimiter2();
		echo "Press to see data in ";
		echo "<input type=\"submit\" name=\"action\" value=\"plaintext\">";

		show_table2_header_end();

		show_footer();
	}

	# Insert form for Item information
	function	insert_item_form($itemid)
	{
		if(isset($itemid))
		{
			$result=DBselect("select i.description, i.key_, h.host, h.port, i.delay, i.history, i.status, i.type, i.snmp_community,i.snmp_oid from items i,hosts h where i.itemid=$itemid and h.hostid=i.hostid");
		
			$description=DBget_field($result,0,0);
			$key=DBget_field($result,0,1);
			$host=DBget_field($result,0,2);
			$port=DBget_field($result,0,3);
			$delay=DBget_field($result,0,4);
			$history=DBget_field($result,0,5);
			$status=DBget_field($result,0,6);
			$type=DBget_field($result,0,7);
			$snmp_community=DBget_field($result,0,8);
			$snmp_oid=DBget_field($result,0,9);
		}
		else
		{
			$description="";
			$key="";
			$host="";
			$port=10000;
			$delay=30;
			$history=30*24*3600;
			$status=0;
			$type=0;
			$snmp_community="public";
			$snmp_oid="interfaces.ifTable.ifEntry.ifInOctets.1";
		}

		echo "<br>";

		show_table2_header_begin();
		echo "Item configuration";
 
		show_table2_v_delimiter();
		echo "<form method=\"post\" action=\"items.php\">";
		echo "<input name=\"itemid\" type=hidden value=$itemid size=8>";
		echo "Description";
		show_table2_h_delimiter();
		echo "<input name=\"description\" value=\"$description\"size=40>";

		show_table2_v_delimiter();
		echo "Host";
		show_table2_h_delimiter();
		echo "<select name=\"hostid\" value=\"3\">";
	        $result=DBselect("select hostid,host from hosts order by host");
	        for($i=0;$i<DBnum_rows($result);$i++)
	        {
	                $hostid_=DBget_field($result,$i,0);
	                $host_=DBget_field($result,$i,1);
			if($host==$host_)
			{
	                	echo "<option value=\"$hostid_\" selected>$host_";
			}
			else
			{
	                	echo "<option value=\"$hostid_\">$host_";
			}
	        }
		echo "</select>";

		show_table2_v_delimiter();
		echo "Type";
		show_table2_h_delimiter();
		echo "<SELECT NAME=\"type\" value=\"$type\" size=\"1\">";
		echo "<OPTION VALUE=\"0\"";
		if($type==0) echo "SELECTED";
		echo ">Zabbix agent";
		echo "<OPTION VALUE=\"1\"";
		if($type==1) echo "SELECTED";
		echo ">SNMPv1 agent";
		echo "</SELECT>";
 
		show_table2_v_delimiter();
		echo "SNMP community (for SNMP only)";
		show_table2_h_delimiter();
		echo "<input name=\"snmp_community\" value=\"$snmp_community\" size=16>";

		show_table2_v_delimiter();
		echo "SNMP OID (for SNMP only)";
		show_table2_h_delimiter();
		echo "<input name=\"snmp_oid\" value=\"$snmp_oid\" size=40>";

		show_table2_v_delimiter();
		echo "Key";
		show_table2_h_delimiter();
		echo "<input name=\"key\" value=\"$key\" size=40>";

		show_table2_v_delimiter();
		echo "Delay";
		show_table2_h_delimiter();
		echo "<input name=\"delay\" value=\"$delay\" size=5>";

		show_table2_v_delimiter();
		echo "Keep history (in sec)";
		show_table2_h_delimiter();
		echo "<input name=\"history\" value=\"$history\" size=8>";

		show_table2_v_delimiter();
		echo "Status";
		show_table2_h_delimiter();
		echo "<SELECT NAME=\"status\" value=\"$status\" size=\"1\">";
		echo "<OPTION VALUE=\"0\"";
		if($status==0) echo "SELECTED";
		echo ">Monitored";
		echo "<OPTION VALUE=\"1\"";
		if($status==1) echo "SELECTED";
		echo ">Disabled";
		echo "<OPTION VALUE=\"2\"";
		if($status==2) echo "SELECTED";
		echo ">Trapper";
		echo "<OPTION VALUE=\"3\"";
		if($status==3) echo "SELECTED";
		echo ">Not supported";
		echo "</SELECT>";
 
		show_table2_v_delimiter2();
		echo "<input type=\"submit\" name=\"register\" value=\"add\">";
		if(isset($itemid))
		{
			echo "<input type=\"submit\" name=\"register\" value=\"update\">";
			echo "<input type=\"submit\" name=\"register\" value=\"delete\">";
		}
 
		show_table2_header_end();
?>
<?
?>
</TR>
</TABLE>

</CENTER>
</FORM>

</BODY>
</HTML>
<?
	}

	# Insert form for User
	function	insert_user_form($userid)
	{
		if(isset($userid))
		{
			$result=DBselect("select u.alias,u.name,u.surname,u.passwd,g.groupid from users u,groups g where u.groupid=g.groupid and u.userid=$userid");
	
			$alias=DBget_field($result,0,0);
			$name=DBget_field($result,0,1);
			$surname=DBget_field($result,0,2);
#			$password=DBget_field($result,0,3);
			$password="";
			$groupid_=DBget_field($result,0,4);
		}
		else
		{
			$alias="";
			$name="";
			$surname="";
			$password="";
			$groupid_=0;
		}

		show_table2_header_begin();
		echo "New user";

		show_table2_v_delimiter();
		echo "<form method=\"post\" action=\"users.php\">";
		if(isset($userid))
		{
			echo "<input name=\"userid\" type=\"hidden\" value=\"$userid\" size=8>";
		}
		echo "Alias";
		show_table2_h_delimiter();
		echo "<input name=\"alias\" value=\"$alias\" size=20>";

		show_table2_v_delimiter();
		echo "Name";
		show_table2_h_delimiter();
		echo "<input name=\"name\" value=\"$name\" size=20>";

		show_table2_v_delimiter();
		echo "Surname";
		show_table2_h_delimiter();
		echo "<input name=\"surname\" value=\"$surname\" size=20>";

		show_table2_v_delimiter();
		echo "User group";
		show_table2_h_delimiter();
		echo "<select name=\"groupid\">";
		$result=DBselect("select groupid,name from groups order by name");
		for($i=0;$i<DBnum_rows($result);$i++)
		{
			$groupid=DBget_field($result,$i,0);
			$name=DBget_field($result,$i,1);
			if($groupid_==$groupid)
			{
				echo "<option value=\"$groupid\" selected>$name";
			}
			else
			{
				echo "<option value=\"$groupid\">$name";
			}
		}
		echo "</select>";

		show_table2_v_delimiter();
		echo "Password";
		show_table2_h_delimiter();
		echo "<input type=\"password\" name=\"password1\" value=\"$password\" size=20>";

		show_table2_v_delimiter();
		echo "Password (once again)";
		show_table2_h_delimiter();
		echo "<input type=\"password\" name=\"password2\" value=\"$password\" size=20>";

		show_table2_v_delimiter2();
		echo "<input type=\"submit\" name=\"register\" value=\"add\">";
		if(isset($userid))
		{
			echo "<input type=\"submit\" name=\"register\" value=\"update\">";
			echo "<input type=\"submit\" name=\"register\" value=\"delete\">";
		}

		show_table2_header_end();
	}

	# Insert form for Trigger
	function	insert_trigger_form($hostid,$triggerid)
	{
		if(isset($triggerid))
		{
			$trigger=select_trigger_by_triggerid($triggerid);
	
			$expression=explode_exp($trigger["expression"],0);
			$description=$trigger["description"];
			$priority=$trigger["priority"];
			$istrue=$trigger["istrue"];
			$comments=$trigger["comments"];
			$url=$trigger["url"];
		}
		else
		{
			$expression="";
			$description="";
			$priority=0;
			$istrue=0;
			$comments="";
			$url="";
		}
		
		echo "<br>";

		show_table2_header_begin();
		echo "Trigger configuration";
 
		show_table2_v_delimiter();
		if(isset($hostid))
		{
			echo "<form method=\"post\" action=\"triggers.php?hostid=$hostid\">";
		}
		else
		{
			echo "<form method=\"post\" action=\"triggers.php\">";
		}
		echo "<input name=\"triggerid\" type=hidden value=$triggerid size=8>";
		echo "Description";
		show_table2_h_delimiter();
		echo "<input name=\"description\" value=\"$description\" size=70>";

		show_table2_v_delimiter();
		echo "Expression";
		show_table2_h_delimiter();
		echo "<input name=\"expression\" value=\"$expression\" size=70>";

		show_table2_v_delimiter();
		echo "Priority";
		show_table2_h_delimiter();
		echo "<SELECT NAME=\"priority\" size=\"1\">";
		echo "<OPTION VALUE=\"0\" "; if($priority==0) echo "SELECTED"; echo ">Not classified";
		echo "<OPTION VALUE=\"1\" "; if($priority==1) echo "SELECTED"; echo ">Just for information";
		echo "<OPTION VALUE=\"2\" "; if($priority==2) echo "SELECTED"; echo ">Warning";
		echo "<OPTION VALUE=\"3\" "; if($priority==3) echo "SELECTED"; echo ">Average";
		echo "<OPTION VALUE=\"4\" "; if($priority==4) echo "SELECTED"; echo ">High";
		echo "<OPTION VALUE=\"5\" "; if($priority==5) echo "SELECTED"; echo ">Disaster";
		echo "</SELECT>";

		show_table2_v_delimiter();
		echo "Comments";
		show_table2_h_delimiter();
 		echo "<TEXTAREA NAME=\"comments\" COLS=70 ROWS=\"7\" WRAP=\"SOFT\">$comments</TEXTAREA>";

		show_table2_v_delimiter();
		echo "URL";
		show_table2_h_delimiter();
		echo "<input name=\"url\" value=\"$url\" size=70>";

		show_table2_v_delimiter();
		echo "Disabled";
		show_table2_h_delimiter();
		echo "<INPUT TYPE=\"CHECKBOX\" ";
		if($istrue==2) { echo " CHECKED "; }
		echo "NAME=\"disabled\"  VALUE=\"true\">";

 
		show_table2_v_delimiter2();
		echo "<input type=\"submit\" name=\"register\" value=\"add\">";
		if(isset($triggerid))
		{
			echo "<input type=\"submit\" name=\"register\" value=\"update\">";
			echo "<input type=\"submit\" name=\"register\" value=\"delete\">";
		}

		if(isset($triggerid))
		{
			show_table2_v_delimiter();
			echo "Dependencies";
			show_table2_h_delimiter();
			$sql="select t.triggerid,t.description from triggers t,trigger_depends d where t.triggerid=d.triggerid_up and d.triggerid_down=$triggerid";
			$result1=DBselect($sql);
			echo "<SELECT NAME=\"dependency\" size=\"1\">";
			for($i=0;$i<DBnum_rows($result1);$i++)
			{
				$depid=DBget_field($result1,$i,0);
				$depdescr=DBget_field($result1,$i,1);
				echo "<OPTION VALUE=\"$depid\">$depdescr";
			}
			echo "</SELECT>";

			show_table2_v_delimiter();
			echo "New dependency";
			show_table2_h_delimiter();
			$sql="select t.triggerid,t.description from triggers t where t.triggerid!=$triggerid order by t.description";
			$result=DBselect($sql);
			echo "<SELECT NAME=\"depid\" size=\"1\">";
			for($i=0;$i<DBnum_rows($result);$i++)
			{
				$depid=DBget_field($result,$i,0);
				$depdescr=DBget_field($result,$i,1);
				echo "<OPTION VALUE=\"$depid\">$depdescr";
			}
			echo "</SELECT>";

			show_table2_v_delimiter2();
			if(isset($triggerid))
			{
				echo "<input type=\"submit\" name=\"register\" value=\"add dependency\">";
				if(DBnum_rows($result1)>0)
				{
					echo "<input type=\"submit\" name=\"register\" value=\"delete dependency\">";
				}
			}
		}

		echo "</form>";
		show_table2_header_end();
	}


	function	show_footer()
	{
		echo "<br>";
		echo "<table border=0 cellpadding=1 cellspacing=0 width=\"100%\" align=center>";
		echo "<tr>";
		echo "<td bgcolor=\"#000000\">";
		echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\" width=\"100%\" bgcolor=\"#666666\">";
		echo "<tr><td align=center>";
		echo "<font face=\"Arial,Helvetica\" size=1>";
		echo "<a href=\"http://zabbix.sourceforge.net\">ZABBIX</a> Copyright 2000,2001,2002 by <a href=\"mailto:alex@gobbo.caves.lv\">Alexei Vladishev</a>";
		echo "</font>";
		echo "</td></tr>";
		echo "</table>";
		echo "</td>";
		echo "</tr>";
		echo "</table>";

		echo "</body>";
	}

	function	get_stats()
	{
	        $result=DBselect("select count(*) from history");
		$stat["history_count"]=DBget_field($result,0,0);

	        $result=DBselect("select count(*) from alarms");
		$stat["alarms_count"]=DBget_field($result,0,0);

	        $result=DBselect("select count(*) from alerts");
		$stat["alerts_count"]=DBget_field($result,0,0);

	        $result=DBselect("select count(*) from triggers");
		$stat["triggers_count"]=DBget_field($result,0,0);

	        $result=DBselect("select count(*) from triggers where istrue!=2");
		$stat["triggers_count_enabled"]=DBget_field($result,0,0);

	        $result=DBselect("select count(*) from triggers where istrue=2");
		$stat["triggers_count_disabled"]=DBget_field($result,0,0);

	        $result=DBselect("select count(*) from items");
		$stat["items_count"]=DBget_field($result,0,0);

	        $result=DBselect("select count(*) from items where status=0");
		$stat["items_count_active"]=DBget_field($result,0,0);

	        $result=DBselect("select count(*) from items where status=1");
		$stat["items_count_not_active"]=DBget_field($result,0,0);

	        $result=DBselect("select count(*) from items where status=3");
		$stat["items_count_not_supported"]=DBget_field($result,0,0);

	        $result=DBselect("select count(*) from items where status=2");
		$stat["items_count_trapper"]=DBget_field($result,0,0);

	        $result=DBselect("select count(*) from hosts");
		$stat["hosts_count"]=DBget_field($result,0,0);

	        $result=DBselect("select count(*) from hosts where status=0");
		$stat["hosts_count_monitored"]=DBget_field($result,0,0);

	        $result=DBselect("select count(*) from hosts where status=1");
		$stat["hosts_count_not_monitored"]=DBget_field($result,0,0);

	        $result=DBselect("select count(*) from users");
		$stat["users_count"]=DBget_field($result,0,0);


		return $stat;
	}

	function	calculate_availability($triggerid)
	{
	        $result=DBselect("select count(*),min(clock),max(clock) from alarms where triggerid=$triggerid");
		if(DBget_field($result,0,0)>0)
		{
			$min=DBget_field($result,0,1);
			$max=DBget_field($result,0,2);
		}
		else
		{
			$max=time();
			$min=$max-24*3600;
		}

		$sql="select clock,istrue from alarms where triggerid=$triggerid and clock>=$min and clock<=$max";
		$result=DBselect($sql);

// -1,0,1
		$state=-1;
		$true_time=0;
		$false_time=0;
		$time=$min;
		$max=time();
		for($i=0;$i<DBnum_rows($result);$i++)
		{
			$clock=DBget_field($result,$i,0);
			$istrue=DBget_field($result,$i,1);

			$diff=$clock-$time;

			$time=$clock;

			if($state==-1)
			{
				$state=$istrue;
				if($state == 0)
				{
					$false_time+=$diff;
				}
				if($state == 1)
				{
					$true_time+=$diff;
				}
			}
			else if($state==0)
			{
				$false_time+=$diff;
				$state=$istrue;
			}
			else if($state==1)
			{
				$true_time+=$diff;
				$state=$istrue;
			}
		}

		if(DBnum_rows($result)==0)
		{
			$false_time=$max-$min;
		}
		else
		{
			if($state==0)
			{
				$false_time=$false_time+$max-$time;
			}
			else
			{
				$true_time=$true_time+$max-$time;
			}
		}

		$ret["true_time"]=$true_time;
		$ret["false_time"]=$false_time;
		$ret["true"]=(100*$true_time)/($true_time+$false_time);
		$ret["false"]=(100*$false_time)/($true_time+$false_time);
		return $ret;
	}

?>
